//
//  XMLhelpers.m
//  tjp_helper
//
//  Created by Daniel Braun on 28/01/07.
//  Copyright 2007 Daniel Braun http://braun.daniel.free.fr. All rights reserved.
//

#import "XMLhelpers.h"

@implementation XmlAccessor

- (void) dealloc {
	[target_field release];
	[target_attrib release];
	[super dealloc];
}
- (id) init {
	self = [super init];
	if (self != nil) {
		//<#initializations#>
	}
	return self;
}
- (void) setField:(NSString *)s attrib:(NSString *)a xmlConverter:(SEL) c1 stringConverter:(SEL)c2
{
	[target_field release];
	target_field=[s retain];
	[target_attrib release];
	target_attrib=[a retain];
	xmlConverter=c1;
	stringConverter=c2;
}
+ (id)  xmlAccessorWith:(NSString *)s attrib:(NSString *)a xmlConverter:(SEL) c1 stringConverter:(SEL)c2
{
	XmlAccessor *xa=[[[[self class] alloc]init]autorelease];
	[xa setField:s attrib:a xmlConverter:c1 stringConverter:c2];
	return xa;
}

- (id) applyWith:(NSXMLElement*)elem
{
	NSString *v=nil;
	if (target_field) {
		elem=firstValueForXML(elem, target_field);
	}
	if (target_attrib) {
		v=[[elem attributeForName:target_attrib] stringValue];
	} else {
		v=[elem stringValue];
	}
	if (xmlConverter) {
		id r=[elem performSelector:xmlConverter];
		return r;
	}
	if (stringConverter) {
		id r=[v performSelector:stringConverter];
		return r;
	}
	return v;
}

@end